package com.blog.controller;

import com.blog.annotation.UnderlineToCamel;
import com.blog.check.result.Result;
import com.blog.check.resultenum.UserChkRsEnum;
import com.blog.dao.query.BlogQuery;
import com.blog.form.BlogForm;
import com.blog.service.service.BlogService;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 博客控制器
 *
 * @author 泡面ing
 * @date 2021/11/25
 */
@Controller
@RequestMapping("/blog")
public class BlogController {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    private BlogService blogService;

    @Autowired
    public void setBlogService(BlogService blogService){
        this.blogService = blogService;
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Result list(@UnderlineToCamel BlogQuery blogQuery,HttpServletResponse response){
        Result result;
        try{
            result = blogService.list(5,10,blogQuery);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("查询信息异常",e);
        }
        return result;
    }

    @RequestMapping( value = "/{blog_id}",method = RequestMethod.GET)
    @ResponseBody
    public Result get(@PathVariable("blog_id") String blogId, HttpServletResponse response, HttpServletRequest request){
        Result result;
        try {
            request.getAuthType();
            result = blogService.get(blogId,request);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("查询信息异常",e);
        }
        return result;
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody BlogForm blogForm,  HttpServletResponse response){
        Result result;
        try{
            result = blogService.save(blogForm);
            if (result.getCode() == UserChkRsEnum.USER_IS_NOT_LOGIN.getCode()){
                response.setStatus(HttpStatus.SC_UNAUTHORIZED);
                return result;
            }
            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_CREATED);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("保存信息异常" ,e);
        }
        return result;
    }

    @RequestMapping(value = "/{blog_id}",method = RequestMethod.PUT)
    @ResponseBody
    public Result update(@PathVariable("blog_id") String blogId,@RequestBody BlogForm blogForm, HttpServletResponse response){
        Result result;
        try{
            result = blogService.update(blogForm,blogId);
            if (result.getCode() == UserChkRsEnum.USER_IS_NOT_LOGIN.getCode()){
                response.setStatus(HttpStatus.SC_UNAUTHORIZED);
                return result;
            }
            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_CREATED);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("更新信息异常" ,e);
        }
        return result;
    }

    @RequestMapping(value = "/{blog_id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Result remove(@PathVariable("blog_id") String blogId,HttpServletResponse response){
        Result result;
        try{
            result = blogService.remove(blogId);
            if (result.getCode() == UserChkRsEnum.USER_IS_NOT_LOGIN.getCode()){
                response.setStatus(HttpStatus.SC_UNAUTHORIZED);
                return result;
            }
            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_CREATED);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("删除信息异常" ,e);
        }
        return result;
    }

}
